<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/9/3
 * Time: 下午4:58
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class UeeshopApi extends OAuth_Controller
{

    public function cate()
    {
        $this->load->model('siteweb_model');
        $this->load->library('sys_message');
        $assoc = $this->uri->uri_to_assoc();

        $web_id = intval($assoc['web_id']);
        $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_id));
        !$siteweb && $this->sys_message->wrong('网站不存在!');

        $this->load->library('ueeshop_api', $siteweb);

        $ueeshop_cate = $this->ueeshop_api->get_cate();

        if ($ueeshop_cate['msg']) {
            $this->siteweb_model->insert_siteweb(array('cate' => json_encode($ueeshop_cate['msg'])), $web_id);
        }

        $this->sys_message->wrong('成功!');

//        print_r($this->ueeshop_api->get_attr());
//
//        print_r($this->ueeshop_api->get_warehouse());
//
//        print_r($this->ueeshop_api->get_attr_val(137));
    }

    public function get_goods()
    {
        $this->load->model('siteweb_model');
        $this->load->library('sys_message');

        $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => 1));
        !$siteweb && $this->sys_message->wrong('网站不存在!');

        $this->load->library('ueeshop_api', $siteweb);

        print_r($this->ueeshop_api->get_goods('ML2296'));
    }

    public function up_product()
    {

        $this->load->model('siteweb_model');
        $this->load->model('goods_model');
        $this->load->model('shopify_goods_model');
        $this->load->model('goods_sku_model');
        $this->load->library('sys_message');
        $this->load->model('tagslist_model');
        $assoc = $this->uri->uri_to_assoc();

        $goods_id = intval($assoc['goods_id']);
        $web_id = intval($_POST['web_id']);

        $goods = $this->goods_model->goods_info($goods_id);
        !$goods && $this->sys_message->wrong('产品不存在!');


        if ($web_id) {

            $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_id));
            !$siteweb && $this->sys_message->wrong('网站不存在!');

            $this->load->library('ueeshop_api', $siteweb);

            $shopify_goods = $this->shopify_goods_model->goods_info(array('goods_id' => $goods_id, 'web_id' => $web_id));
            //$shopify_goods && $this->sys_message->wrong('产品已经上传过了!');

            if ($shopify_goods) {
                $sg_id = $shopify_goods['sg_id'];
                print_r(unserialize($shopify_goods['variants']));
            } else {

                //uee Attr

                $goods_sku = $this->goods_model->get_goods_sku($goods_id, 1);
                !$goods_sku && $this->sys_message->wrong('SKU不存在!');

                $_variants['ExtAttr'] = array();
                foreach ($goods_sku as $row) {
                    $_color[str_replace(array(' ', '.', '/', '(', ')'), '',$row['color'])] = $row['color'];
                    $_size[str_replace(array(' ', '.', '/', '(', ')'), '',$row['size'])] = $row['size'];
                    $_variants['ExtAttr'][] = array(
                        "133:{$row['size']};132:{$row['color']}",
                        $goods['shop_price'],
                        999,
                        $goods['goods_weight'] / 1000,
                        $siteweb['ab_name'] . $row['sku_num_sn'],
                        0,
                        1
                    );
                }

                $_variants['Attr']['132'] = array_filter(array_values($_color));
                $_variants['Attr']['133'] = array_filter(array_values($_size));

                $data = array(
                    'goods_id' => $goods['goods_id'],
                    'goods_sn' => $siteweb['ab_name'] . $goods['goods_id'],
                    'web_id' => $web_id,
                    'goods_name' => "{$siteweb['title']} " . $goods['goods_name'],
                    'cat_id' => $goods['cat_id'],
                    'shop_price' => $goods['shop_price'],
                    'market_price' => $goods['market_price'],
                    'body_html' => $goods['body_html'],
                    'is_sales' => 1,
                    'variants' => $_variants,
                    'tags' => ''
                );

                $goods['goods_attr'] = json_decode($goods['goods_attr'], true);
                if ($goods['goods_attr'] && count($goods['goods_attr']['attr_val']) > 1) {
                    $html = '<table><tr>';
                    foreach ($goods['goods_attr']['attr_title'] as $attr_title) {
                        $html .= '<td>' . $attr_title . '</td>';
                    }
                    $html .= '</tr>';

                    $_attr = array();
                    foreach ($goods['goods_attr']['attr_val'] as $k => $_row) {
                        foreach ($_row as $_k => $_row_attr) {
                            $_attr[$_k][$k] = $_row_attr;
                        }
                    }

                    foreach ($_attr as $k_val => $_attr_val) {
                        $html .= '<tr>';
                        foreach ($_attr_val as $k_title => $attr_title) {
                            if ($k_title == 0) {
                                $html .= '<td>' . $attr_title . '</td>';
                            } else {
                                $attr_val = explode('-', $attr_title);

                                $a1 = sprintf('%.1f', $attr_val[0] / 2.54);
                                $a2 = $attr_val[1] ? ("-" . sprintf('%.1f', $attr_val[1] / 2.54)) : '';
                                $html .= '<td>' . $a1 . $a2 . '(' . $attr_title . 'cm)' . '</td>';
                            }
                        }
                        $html .= '</tr>';
                    }

                    $html .= '</table>';
                    $data['body_html'] .= $html;
                }


                $sg_id = $this->shopify_goods_model->insert_goods($data);
            }


            if ($sg_id) {

                $shopify_data = $this->shopify_goods_model->up_shopify_data($sg_id);
                $shopify_data['cate_id'] = intval($this->input->post('cate_id'));
                $shopify_data['goods_weight'] = $goods['goods_weight'];
                $response = $this->ueeshop_api->add_goods($shopify_data);

                if ($response['msg'] == 'success' || @$response['msg']['ms'] == 'success') {

                    $shopify_goods_data = array(
                        'shopify_id' => $response['msg']['ProId'],
                        'update_time' => 0
                    );
                    $this->shopify_goods_model->update_goods($sg_id, $shopify_goods_data);

                    $this->sys_message->success('成功!');
                }

            }

            $this->sys_message->wrong('失败！');

        } else {

            $siteweb = $this->siteweb_model->siteweb_list(array('type' => 3));

            $html = '<select id="a_web_id" class="swal2-select form-control" name="web_id"><option value="0">选择站点</option>';
            $a_html = '';
            foreach ($siteweb as $row) {
                $html .= '<option value="' . $row['web_id'] . '">' . $row['web_name'] . '</option>';
                $a_html .= '<input id="web_' . $row['web_id'] . '" type="hidden" value=' . "'" . $row['cate'] . "'" . ' />';
            }

            $html .= '</select><select id="a_cate_id" class="swal2-select form-control" name="cate_id"><option value="0">选择分类</option></select>' . $a_html;

            echo $html;
            exit;
        }
    }

    public function batch_upload()
    {

        $this->load->library('sys_message');

        if ($_POST) {

            $this->load->model('goods_model');
            $this->load->model('goods_sku_model');
            $this->load->model('siteweb_model');
            $this->load->model('shopify_goods_model');


            $goods_sn = array_filter(explode(PHP_EOL, $this->input->post('goods_sn')));
            $cate_id = intval($this->input->post('cate_id'));
            $web_id = intval($this->input->post('web_id'));

            !$web_id && $this->sys_message->wrong('请选择站点！');
            !$cate_id && $this->sys_message->wrong('请选择分类!');
            !$goods_sn && $this->sys_message->wrong('请选择货号!');

            $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_id));
            !$siteweb && $this->sys_message->wrong('网站不存在!');

            $this->load->library('ueeshop_api', $siteweb);

            $goods_list = $this->goods_model->siteweb_goods($goods_sn);

            foreach ($goods_list['goods'] as $goods) {
                $goods_sku = $goods_list['sku'][$goods['goods_id']];

                $_color = array();
                $_size = array();
                $_variants['ExtAttr'] = array();
                foreach ($goods_sku as $row) {
                    $_color[$row['color']] = $row['color'];
                    $_size[$row['size']] = $row['size'];
                    $_variants['ExtAttr'][] = array("132:{$row['color']};133:{$row['size']}",$goods['shop_price'],999,$goods['goods_weight'],$siteweb['ab_name'].$goods_sku['sku_num_sn'],0,1);
                }

                $_variants['Attr']['132'] = array_filter(array_values($_color));
                $_variants['Attr']['133'] = array_filter(array_values($_size));



                $data = array(
                    'goods_id' => $goods['goods_id'],
                    'goods_sn' => $siteweb['ab_name'] . $goods['goods_sn'],
                    'web_id' => $web_id,
                    'goods_name' => "{$siteweb['title']} " . $goods['goods_name'],
                    'cat_id' => $goods['cat_id'],
                    'shop_price' => $goods['shop_price'],
                    'market_price' => $goods['market_price'],
                    'body_html' => $goods['body_html'],
                    'is_sales' => 1,
                    'variants' => $_variants,
                    'tags' => ''
                );

                $goods['goods_attr'] = json_decode($goods['goods_attr'], true);

                if ($goods['goods_attr'] && count($goods['goods_attr']['attr_val']) > 1) {
                    $html = '<table><tr>';
                    foreach ($goods['goods_attr']['attr_title'] as $attr_title) {
                        $html .= '<td>' . $attr_title . '</td>';
                    }
                    $html .= '</tr>';

                    $_attr = array();
                    foreach ($goods['goods_attr']['attr_val'] as $k => $_row) {
                        foreach ($_row as $_k => $_row_attr) {
                            $_attr[$_k][$k] = $_row_attr;
                        }
                    }

                    foreach ($_attr as $k_val => $_attr_val) {
                        $html .= '<tr>';
                        foreach ($_attr_val as $k_title => $attr_title) {
                            if ($k_title == 0) {
                                $html .= '<td>' . $attr_title . '</td>';
                            } else {
                                $attr_val = explode('-', $attr_title);

                                $a1 = sprintf('%.1f', $attr_val[0] / 2.54);
                                $a2 = $attr_val[1] ? ("-" . sprintf('%.1f', $attr_val[1] / 2.54)) : '';
                                $html .= '<td>' . $a1 . $a2 . '(' . $attr_title . 'cm)' . '</td>';
                            }
                        }
                        $html .= '</tr>';
                    }

                    $html .= '</table>';
                    $data['body_html'] .= $html;
                }


                $sg_id = $this->shopify_goods_model->insert_goods($data);

                if ($sg_id) {

                    $shopify_data = $this->shopify_goods_model->up_shopify_data($sg_id);
                    $shopify_data['cate_id'] = $cate_id;
                    $shopify_data['goods_weight'] = $goods['goods_weight'];

                    $response = $this->ueeshop_api->add_goods($shopify_data);

                    if ($response['msg']['ms'] == 'success') {

                        $shopify_goods_data = array(
                            'shopify_id' => $response['msg']['ProId'],
                            'update_time' => 0
                        );
                        $this->shopify_goods_model->update_goods($sg_id, $shopify_goods_data);
                    }

                }
            }

            $this->sys_message->success('成功!');


        }
    }

}
